struct CPU {
    current_operation: u16, //操作码u16类型
    registers: [u8; 2],     //加法运算，两个寄存器
}
impl CPU {
    //如果从内存读会很复杂
    fn read_opcode(&self) -> u16 {
        self.current_operation
    }

    fn run(&mut self) {
        let opcode = self.read_opcode();

        let c = ((opcode & 0xF000) >> 12) as u8;
        let x = ((opcode & 0x0F00) >> 8) as u8;
        let y = ((opcode & 0x00F0) >> 4) as u8;
        let d = ((opcode & 0x000F) >> 0) as u8;

        match (c, x, y, d) {
            (0x8, _, _, 0x4) => self.add_xy(x, y), //匹配元组，分配到“硬件电路”
            _ => todo!("opcode{:04x}", opcode),    //需要补充
        }
    }

    fn add_xy(&mut self, x: u8, y: u8) {
        self.registers[x as usize] += self.registers[y as usize]; //将值存到x寄存器
    }
}
fn main() {
    let mut cpu = CPU {
        current_operation: 0,
        registers: [0; 2],
    };
    cpu.current_operation = 0x8014; // 8:使用两个寄存器，0：寄存器0，1：寄存器1，4：加法运算
    cpu.registers[0] = 5;
    cpu.registers[1] = 10;

    cpu.run();
    assert_eq!(cpu.registers[0], 15);
    println!("5 + 10 = {}", cpu.registers[0]);
}
